require('async-to-gen/register')
var rd = require('rd');
const util = require('util')
var moment = require('moment')
var UpYun = require('upyun');
var path = require('path')
var Bucket = "pingo1"
var Username = "pingo"
var Passwd = "pin88888888"
var Notify = "http://www.upyun.com/notify"
var Secret = "LxEJGmFt51z6h6Z5lDw2Hz+IEG4="
var mRoot = "/m.chaopaikeji.com"
var ServiceURI = "http://pingo1.b0.upaiyun.com"

var Sequelize = require('sequelize');
var sequelize = new Sequelize('sqlite:spider-pic.sqlite3', {
    dialect: 'sqlite',
    storage: './spider-pic.sqlite3',
});

// util.log("%s", Bucket)
var upyun = new UpYun(Bucket, Username, Passwd, 'v0.api.upyun.com', {
    apiVersion: 'v2',
    secret: Secret
})

// var files = rd.readSync('imgs/zhifu')

// for (let f of files) {
//     util.log('file: %s', f);
//     var attStr = f.match(/\/imgs\/\w+\/\w+\//g)
//     util.log(attStr)
//     var attArray = String(attStr).split("/")
//     util.log(attArray)
//     util.log("category:%s, album:%s", attArray[2], attArray[3])

//     //upload upyun
//     var x =  util.format("%s/%d/%d/%d/%d%s", mRoot, moment().format("YYYY"), moment().format("MM"), moment().format("DD"),
//         moment().format("X"), path.extname(f))

//     util.log("%s%s", ServiceURI, x)    
// }

var album = sequelize.define('album', {
    id: {
        type: Sequelize.BIGINT,
        field: 'id',
        autoIncrement: true,
        primaryKey: true //定义为主键
    },
    category: {
        type: Sequelize.STRING,
        field: 'category'
    },
    album: {
        type: Sequelize.STRING,
        field: 'album'
    },
    upyun: {
        type: Sequelize.STRING,
        field: 'upyun'
    },
}, {
        // tableName:'tbl_author',
        timestamps: true,

        //sequelize会在模型定义的时候自动添加createdAt和updatedAt属性，这样我们可以知道每行记录的变更时间
        //如果不想要这个特性，可以把timestamps置为false
        // timestamps: false,

        //不要删除记录，而是添加一个deletedAt属性，只有在timestamps启用的情况下才有效。
        paranoid: true,


        //不对自动添加的属性使用驼峰命名法，而是用下划线风格。
        //因此updatedAt变成udpated_at
        underscored: true,

        //禁止修改表名，sequelize默认会把所有模型的名字（define方法的第一个参数）转成复数
        //不需要的话就设成true，这样模型名就是表名
        //   freezeTableName: true,

        //自定义表名
        //tableName: 'my_very_custom_table_name'

    });

util.log(album)
sequelize.sync()
    .then(function () {
        util.log('synced')
    })
rd.eachFileSync('imgs/zhifu', function (f, s) {
    util.log('file: %s', f);
    var attStr = f.match(/\/imgs\/\w+\/\w+\//g)
    util.log(attStr)
    var attArray = String(attStr).split("/")
    util.log(attArray)
    util.log("category:%s, album:%s", attArray[2], attArray[3])

    //upload upyun
    var x = util.format("%s/%d/%d/%d/%d%s", mRoot, moment().format("YYYY"), moment().format("MM"), moment().format("DD"),
        moment().format("X"), path.extname(f))

    util.log("%s%s", ServiceURI, x)

    upyun.putFile(x, f, null, true, null, function (err, result) {
        if (err) {
            uitl.log(err)
        }
        util.log(result)
    });
    //sql入库
    album.create(JSON.parse(util.format('{"category":"%s", "album":"%s", "upyun":"%s%s"}', attArray[2], attArray[3], ServiceURI, x)))
        .then(function(result) {
            util.log(result)
        })

});


